#include "exercise_radar_car.h"

#include "gd32f4xx.h"
#include "systick.h"
#include <stdio.h>
#include <stdint.h>
#include "gpio_buzzer.h"

#include <string.h>
#include "key.h"
#include "yudie.h"
#include "matrix_button.h"
#include "yudie.h"
#include <stdlib.h>
//
#include "led_driver.h"
#include "wkup_driver.h"
#include "single_btn_driver.h"
#include "buzzer_driver.h"
#include "matrix_btn_driver.h"
#include "usart0_driver.h"
#include "usart1_driver.h"
#include "exti0_driver.h"
#include "exti1_driver.h"
#include "hr_sc04_driver.h"
#include "nixie_driver.h"
#include "timer5_driver.h"

float distance = 0.0f;
void onTimer5Interrupt(void)
{
    distance = hr_sc04_driver_get_distance_mine();
    printf("onTimer5Interrupt distance = %09.02f\n", distance);
}

void float_to_array(float num, uint8_t *num_arr, FlagStatus *dps)
{
    char arr[10];
    int len = 10;

    snprintf(arr, len, "%09.02f\n", num);

    int i;
    int j = 0;
    for (i = 0; i < len; i++)
    {

        if (arr[i] == '.')
        {
            // dot_index = i;
            //  break;
            dps[i] = 1;
        }
        else if (arr[i] == '\0')
        {
        }
        else
        {
            num_arr[j++] = arr[i];
        }
    }
}

void distance_process(float dis, uint8_t *arr)
{
    int int_part, decimal_part;
    float round_dis = ((int)(dis * 100 + 0.5)) / 100.0f;
    int_part = (int)round_dis;
    decimal_part = (round_dis - ((int)round_dis)) * 100;

    // 组成可供数码管显示的数组{-,-,1,2,3,-,4,5}
    arr[0] = 10; // 10表示显示-
    arr[1] = 10; // 10表示显示-

    if (int_part > 99) // 三位数
    {
        arr[2] = int_part / 100;        // 百位
        arr[3] = (int_part % 100) / 10; // 十位
        arr[4] = int_part % 10;         // 个位
    }
    else if (int_part > 9) // 两位数
    {
        arr[2] = 10;            // 显示-
        arr[3] = int_part / 10; // 十位
        arr[4] = int_part % 10; // 个位
    }
    else
    {
        arr[2] = 10;
        arr[3] = 10;
        arr[4] = int_part;
    }

    arr[5] = 10; // 显示- 整数和小数的分隔
    if (decimal_part > 9)
    {
        arr[6] = decimal_part / 10; // 十位
        arr[7] = decimal_part % 10; // 个位
    }
    else
    {
        arr[6] = 0;
        arr[7] = decimal_part;
    }
}

uint8_t num_arr[8] = {0};
FlagStatus dots[8] = {0};

void display_delay_1us()
{
    int j;
    for (j = 0; j < 3; j++) // 消耗432us
    {
        //display(num_arr, dots);
			nixie_driver_display(num_arr,8,dots);
    }
}

void m1()
{
    systick_config();
    // led_driver_config();
    // wkup_driver_config();
    // single_btn_driver_config();
    buzzer_driver_config();
    // matrix_btn_driver_config();
    usart0_driver_tx_config();
    // usart0_driver_rx_config();
    // exti0_driver_config();
    hr_sc04_driver_config();
    nixie_driver_config();
    // nixie_config();

    timer5_driver_config();

    while (1)
    {
        // led_driver_test();
        // wkup_driver_test();
        // single_btn_driver_test();
        // buzzer_driver_test();
        // playYuDie();
        // matrix_btn_driver_test();
        // usart0_driver_tx_test();
        /// hr_sc04_driver_test();
        // nixie_driver_test();
        // nixie_driver_display();

        printf("distance = %09.02f\n", distance);
        // delay_1ms(1000);

        uint8_t num_arr[8] = {0};
        FlagStatus dots[8] = {0};

        // float_to_array(distance, num_arr, dots);
        distance_process(distance, num_arr);

        //        for (int i = 0; i < sizeof(num_arr) / sizeof(uint8_t); i++)
        //        {
        //            printf("%c,", num_arr[i]);
        //        }

        //        printf("\n");

        //        for (int i = 0; i < sizeof(dots) / sizeof(FlagStatus); i++)
        //        {
        //            printf("%d,", dots[i]);
        //        }
        //				 printf("---------------------\n");

        // uint8_t num_arr[8] = {1, 2, 3, 4, 5, 6, 7, 8};
        // FlagStatus dots[8] = {0, 0, 0, 0, 0, 0, 0, 0};

        // delay_1ms(1000);
        int i;
        for (i = 0; i < 8000; i++)
        {
            nixie_driver_display(num_arr, 8, dots);
            // display(num_arr, dots);
        }

        // nixie_driver_display(num_arr, sizeof(num_arr) / sizeof(uint8_t), dots);
    }
}

void m2()
{
    systick_config();
    // led_driver_config();
    // wkup_driver_config();
    // single_btn_driver_config();
    buzzer_driver_config();
    // matrix_btn_driver_config();
    usart0_driver_tx_config();
    // usart0_driver_rx_config();
    // exti0_driver_config();
    hr_sc04_driver_config();
    nixie_driver_config();
    // nixie_config();

    timer5_driver_config();

    uint32_t i;
    //uint8_t j;

    while (1)
    {
        printf("distance = %09.02f\n", distance);

        distance_process(distance, num_arr);

        for (i = 0; i < 50; i++)
        {
            buzzer_driver_vibrate_by_freq_with_delay(L1, display_delay_1us);
            //    // 蜂鸣器通电，PB9输出高电平
            //    gpio_bit_set(GPIOB, GPIO_PIN_9);
            //    for(j = 0; j< 3; j++)       // 消耗432us
            //    {
            //        display(num_arr, dots);
            //    }

            //    // 蜂鸣器断电，PB9输出低电平
            //    gpio_bit_reset(GPIOB, GPIO_PIN_9);
            //    for(j = 0; j< 3; j++)       // 消耗432us
            //    {
            //        display(num_arr, dots);
            //    }
        }

        //
        int count = 0;
        int dis = distance;
        if (dis < 3)
        {
            // led_8_all_off();
            count = 20;
        }
        else if (dis < 6)
        {
            // led_8_all_off();
            // led_8_on_by_index(LED1);
            count = 50;
        }
        else if (dis < 9)
        {
            // led_8_all_off();
            // led_8_on_by_index(LED1);
            // led_8_on_by_index(LED2);
            count = 100;
        }
        else if (dis < 12)
        {
            // led_8_all_off();
            // led_8_on_by_index(LED1);
            // led_8_on_by_index(LED2);
            // led_8_on_by_index(LED3);
            count = 200;
        }
        else if (dis < 15)
        {
            // led_8_all_off();
            // led_8_on_by_index(LED1);
            // led_8_on_by_index(LED2);
            // led_8_on_by_index(LED3);
            // led_8_on_by_index(LED4);
            count = 300;
        }
        else if (dis < 18)
        {
            // led_8_all_off();
            // led_8_on_by_index(LED1);
            // led_8_on_by_index(LED2);
            // led_8_on_by_index(LED3);
            // led_8_on_by_index(LED4);
            // led_8_on_by_index(LED5);
            count = 400;
        }
        else if (dis < 21)
        {
            // led_8_all_off();
            // led_8_on_by_index(LED1);
            // led_8_on_by_index(LED2);
            // led_8_on_by_index(LED3);
            // led_8_on_by_index(LED4);
            // led_8_on_by_index(LED5);
            // led_8_on_by_index(LED6);
            count = 600;
        }
        else if (dis < 24)
        {
            // led_8_all_off();
            // led_8_on_by_index(LED1);
            // led_8_on_by_index(LED2);
            // led_8_on_by_index(LED3);
            // led_8_on_by_index(LED4);
            // led_8_on_by_index(LED5);
            // led_8_on_by_index(LED6);
            // led_8_on_by_index(LED7);
            count = 800;
        }
        else if (dis < 27)
        {
            // led_8_all_on();
            count = 1000;
        }
        else if (dis < 30)
        {
            // led_8_all_on();
            count = 1000;
        }
        else
        {
            // led_8_all_on();
            count = 1000;
        }

        int k;
        for (k = 0; k < count; k++)
        {
            //display(num_arr, dots);
            nixie_driver_display(num_arr,8,dots);
        }
    }
}

void exercise_radar_main(void)
{
    // systick_config();
    // // led_driver_config();
    // // wkup_driver_config();
    // // single_btn_driver_config();
    // buzzer_driver_config();
    // // matrix_btn_driver_config();
    // usart0_driver_tx_config();
    // // usart0_driver_rx_config();
    // // exti0_driver_config();
    // hr_sc04_driver_config();
    // // nixie_driver_config();
    // nixie_config();

    // timer5_driver_config();

    // while(1){
    //     uint32_t b = get_current_time();
    //     display(num_arr, dots);
    //     uint32_t e = get_current_time();
    //     printf("shicha = %ld\n", e - b);//137

    //     delay_1ms(1000);
    // }
    m2();
}
